package com.itita.ww2.handler.requesthandler.basemap;

import java.sql.SQLException;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.smartfoxserver.v2.annotations.MultiHandler;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import com.smartfoxserver.v2.extensions.SFSExtension;
import com.itita.ww2.core.MessageHandler;
import com.itita.ww2.core.ServerService;
import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.enums.WW2ExceptionCode;
import com.itita.ww2.utils.handler.MultiHandlerUtil;

@MultiHandler
public class BaseMapHandler extends BaseClientRequestHandler {
	private static final Logger logger = LoggerFactory.getLogger(BaseMapHandler.class);
	//
	public static final String COMMAND_PREFIX = "basemap"; // 前缀
	//
	//	private static final String CMD_MAPUNITPLACE = "mapunitplace"; // 放置单位到地图
	//	private static final String CMD_MAPUNITUPDATE = "mapunitupdate"; // 更新地图单位数据
	//	private static final String CMD_MAPUNITREMOVE = "mapunitremove"; // 移除地图单位
	//	private static final String CMD_MAPDEFUPGRADE = "mapdefupgrade"; // 防御建筑升级
	//	private static final String CMD_MAPDEFUPGRADECOMPLETE = "mapdefupgradecomplete"; // 防御建筑升级完成
	//	private static final String CMD_GETUSERMAPDATA = "getusermapdata"; // 获取用户地图数据
	// fields
	private MultiHandlerUtil mhu;

	public BaseMapHandler() {
		mhu = new MultiHandlerUtil();

		//		mhu.addClientRequestItem(CMD_MAPUNITPLACE, MapUnitPlace.class);
		//		mhu.addClientRequestItem(CMD_MAPUNITUPDATE, MapUnitUpdate.class);
		//		mhu.addClientRequestItem(CMD_MAPUNITREMOVE, MapUnitRemove.class);
		//		mhu.addClientRequestItem(CMD_MAPDEFUPGRADE, MapDefenceUpgrade.class);
		//		mhu.addClientRequestItem(CMD_MAPDEFUPGRADECOMPLETE, MapDefUpgradeComplete.class);
		//		mhu.addClientRequestItem(CMD_GETUSERMAPDATA, GetMapdata.class);

		logger.info("[" + this.getClass().getSimpleName() + "] initialized.");
	}

	@Override
	public void handleClientRequest(User arg0, ISFSObject arg1) {
		String requestId = arg1.getUtfString(SFSExtension.MULTIHANDLER_REQUEST_ID);
		trace("[" + arg0.getName() + "]" + " - " + COMMAND_PREFIX + "." + requestId);
		if (requestId == null || (requestId != null && requestId.isEmpty())) {
			return;
		}
		// init
		mhu.init(this, requestId, Arrays.asList(ServerService.getInstance().getSFSDBManager()).toArray());
		// Execute
		try {
			mhu.execute(requestId, COMMAND_PREFIX + "." + requestId, arg0, arg1);
		} catch (WW2Exception e) {
			MessageHandler.getInstance().sendMsg(COMMAND_PREFIX + "." + requestId, e.getRetObj(), arg0, e.getExCode(),
					false);
			logger.error(e.toString());
			// e.printStackTrace();
		} catch (SQLException e) {
			MessageHandler.getInstance().sendMsg(COMMAND_PREFIX + "." + requestId, null, arg0, WW2ExceptionCode.SQLEXCEPTION,
					false);
			e.printStackTrace();
		}
	}

}
